window.BT = window.BT || {};
BT.Topbanner = {
    init : function(options){
        /*  
            options : 不同页面传入不同值，目前接受如下页面形式
            {
                pageType: 'trip',
                tripId : 1
            }
            {
                pageType: 'waypoint',
                tripId : 1,
                waypointId:1
            }
            {
                pageType: 'comment',
                tripId : 1,
                waypointId:1
            }
            {
                pageType: 'user',
                userid : 1
            }
            {
                pageType: 'country_city',
                destType : 1,
                destId:1,
                destName: "xxxx"
            }
            {
                pageType: 'poi',
                destType : 1,
                destId:1,
                destName: "xxxx"
            }
            {
                pageType: 'webview',
                url : "http://xxx"
            }
            {
                pageType: 'explore'
            }
            {
                pageType: 'newtrip',
                tripId : 1,
                spotId : 1,
                mode : "[big|small]"
            }
            {
                pageType: 'spot',
                spotId : 1
            }
        */
        var downloadBanner = $('.m-topbanner'),
            downloadText = downloadBanner.find('.topbanner-icon-text'),
            downloadButton = downloadBanner.find('.topbanner-icon-button'),
            downloadClose = downloadBanner.find('.topbanner-icon-close');

        var ua = navigator.userAgent.toLowerCase(),
            iOS = ( ua.match(/(ipad|iphone|ipod)/g) ? true : false ),
            appType = iOS ? 'ios' : 'android',
            downloadUrl = '/download/?type='+ appType +'&src=mobile',
            urls = BT.OpenApp.parseUrl(options);

        if(BT.OpenApp.isInWx()){
            //在微信里面
            BT.OpenApp.wx.isAppInstalled(function(installed){
                //安装了app
                if(installed){
                    downloadText.addClass('topbanner-icon-text1');
                    downloadButton.text("打开应用");
                    bindBannerClick(1);
                    _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'view', 'NEW HTML5 banner 微信里展示量-安装了app-' + appType + "-" + options.pageType]);
                }else{
                    downloadText.addClass('topbanner-icon-text2');
                    downloadButton.text("立即下载");
                    bindBannerClick(2);
                    _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'view', 'NEW HTML5 banner 微信里展示量-没安装app-' + appType + "-" + options.pageType]);
                }
            });
        }else{
            downloadText.addClass('topbanner-icon-text3');
            downloadButton.text("立即开启");
            bindBannerClick(0);
            _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'view', 'NEW HTML5 banner 微信外展示量-' + appType + "-" + options.pageType]);
        }

        downloadClose.on('click',function(e){
            downloadBanner.hide();
            e.stopPropagation();
        });
        function bindBannerClick(state){
            downloadBanner.on("click", function(e) {
                e.preventDefault();
                if(state == 1){
                    _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'click', 'NEW HTML5 banner 微信里点击量-安装了app-' + appType + "-" + options.pageType]);
                    //在微信里面，且安装了app，调用微信跳转第三方app接口
                    BT.OpenApp.wx.launchApp(urls,function(launchOk){
                        if(launchOk){
                            _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'click', 'NEW HTML5 banner 微信里调起app-成功-' + appType + "-" + options.pageType]);
                        }else{
                            _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'click', 'NEW HTML5 banner 微信里调起app-失败-' + appType + "-" + options.pageType]);
                        }
                    });
                }else if(state == 2){
                    _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'click', 'NEW HTML5 banner 微信里点击量-没安装app-' + appType + "-" + options.pageType]);
                    //在微信里面，没有安装app，则跳转下载url
                    window.location.href = downloadUrl;
                }else if(state == 0){
                    _hmt.push(['_trackEvent', 'NEW HTML5 banner', 'click', 'NEW HTML5 banner 微信外点击量-' + appType + "-" + options.pageType]);
                    //在微信外面，则执行调起和下载
                    BT.OpenApp.other.launchApp(urls.url,function(launchOk){
                        if(launchOk){

                        }else{
                            window.location.href = downloadUrl;
                        }
                    })
                }
            });
        }
    }
};